/**
 * @param {Array<string>} path
 * @interface WatchValue { add: string[], remove: string[] }
 * @param {string|Array<string>|WatchValue} value
 * @returns
 */
export default function(paths, value) {
	let watchValue = ["page", "queryForm", "collapse"]
	if (typeof value === "string") {
		watchValue = [value]
	} else if (Array.isArray(value)) {
		watchValue = value
	} else if (typeof value === "object") {
		watchValue = watchValue.concat(value.add || [])
		watchValue = watchValue.filter(item => !(value.remove || []).includes(item))
	}
	if (paths) {
		window.sessionStorage.setItem(window.location.pathname + "-keep-data", [window.location.pathname, ...paths].join(","))
	}
	return {
		created() {
			const routePath = this.$route.path
			const value = JSON.parse(window.sessionStorage.getItem(routePath) || "{}")
			watchValue.forEach(key => {
				if (value[key]) {
					this[key] = value[key]
				}
			})
			const queryList = this.queryList
			this.queryList = page => {
				queryList.call(this, page)

				window.sessionStorage.setItem(
					routePath,
					JSON.stringify(
						watchValue.reduce((obj, key) => {
							if (this[key] === undefined || this[key] === null || this[key] === "") return obj
							obj[key] = this[key]
							return obj
						}, {}),
					),
				)
			}
		},
	}
}
